package com.sctvcloud.bazhong.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sctvcloud.bazhong.R;

public class LoadingView extends LinearLayout {
    private LinearLayout mLoadingLL;
    private TextView mLoadingTV;
    private TextView mHintTV;
    private ImageView mFailureIV;
    private TextView mFailureTV;
    private LinearLayout mFailureLL;
    private ImageView mProgressBarIV;

    public LoadingView(Context context) {
        super(context);
        init(context);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.loading_view, this);
        mLoadingTV = (TextView) findViewById(R.id.loadingview_tv_loading);
        mLoadingLL = (LinearLayout) findViewById(R.id.loadingview_ll_loading);
//        mProgressBar = (ProgressBar)findViewById(R.id.loadingview_iv_progressbar);
//		Drawable draw  = getResources().getDrawable(R.drawable.progressbar_loading);
//		mProgressBar.setIndeterminateDrawable(draw);
//		mProgressBar.setIndeterminate(true);
        mProgressBarIV = (ImageView) findViewById(R.id.loadingview_iv_progressbar);
        mFailureTV = (TextView) findViewById(R.id.loadingview_tv_failure);
        mFailureLL = (LinearLayout) findViewById(R.id.loadingview_ll_failure);
        mFailureIV = (ImageView) findViewById(R.id.loadingview_iv_failure);

        mLoadingLL.setVisibility(View.VISIBLE);
        mFailureLL.setVisibility(View.GONE);

        mHintTV = (TextView) findViewById(R.id.loadingview_tv_hint);
        mHintTV.setVisibility(View.GONE);
        showProgressBar(true);
    }

    private void showProgressBar(boolean show) {
        if (mProgressBarIV != null) {
            if (show) {
                Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.load_more_refresh);
                mProgressBarIV.startAnimation(anim);
            } else {
                mProgressBarIV.clearAnimation();
            }
            mLoadingLL.setVisibility(show ? VISIBLE : INVISIBLE);
        }
    }

    public void setTextColor(int color) {
        mFailureTV.setTextColor(color);
        mLoadingTV.setTextColor(color);
    }

    private void setLoadingText(String text) {
        mLoadingTV.setText(text);
    }

    private void setFailureText(String text) {
        mFailureTV.setText(text);
    }

    public void showLoading() {
        setVisibility(VISIBLE);
        mFailureLL.setVisibility(View.GONE);
        mHintTV.setVisibility(View.GONE);
        showProgressBar(true);
    }

    public void showFailureFace(String text) {
        mFailureTV.setText(text);
        mFailureLL.setVisibility(View.VISIBLE);
        mHintTV.setVisibility(View.GONE);
        showProgressBar(false);
    }

    public void showTryAgain() {
        mFailureTV.setText(R.string.loading_try_again);
        mFailureLL.setVisibility(View.VISIBLE);
        mHintTV.setVisibility(View.GONE);
        showProgressBar(false);
    }

    public void showFailureFace(int textResId) {
        setVisibility(View.VISIBLE);
        showFailureFace(getContext().getString(textResId));
    }


    public void showFailureFace(int failResId, String text) {
        showProgressBar(false);
        mFailureLL.setVisibility(View.VISIBLE);
        mFailureIV.setImageResource(failResId);
        mFailureTV.setText(text);
        mHintTV.setVisibility(View.GONE);
    }

    public void showHintText(String text) {
//        mLoadingLL.setVisibility(View.GONE);
        showProgressBar(false);
        mFailureLL.setVisibility(View.GONE);
        mHintTV.setVisibility(View.VISIBLE);
        mHintTV.setText(text);
    }

    public void showNoDataHint() {
        String text = getContext().getString(R.string.loading_no_data);
        showHintText(text);
    }

    public void hideLoading() {
        setVisibility(View.GONE);
//        mLoadingLL.setVisibility(View.GONE);
        showProgressBar(false);
        mHintTV.setVisibility(View.GONE);
    }

    public void hideFailureFace() {
        mFailureLL.setVisibility(View.GONE);
        mHintTV.setVisibility(View.GONE);
    }

    public void setOnFailureFaceClickListener(OnClickListener listener) {
        mFailureLL.setOnClickListener(listener);
    }
}
